﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Profile;
using System.Web.Security;
using SubSonic;

public partial class User_Employer_EditUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Lblstatus.Text = "";
        if (!IsPostBack)
        {
            var thisUser = HttpContext.Current.User;
            if (thisUser.Identity.IsAuthenticated)
            {
                if (thisUser.IsInRole("edituser"))
                {
                    RadioButtonListRole.Items[2].Enabled = false;
                }
                else
                {
                    RadioButtonListRole.Items[2].Enabled = true;
                }
            }

            RegButton.Visible = false;
            CancelButton.Visible = false;

            
            SqlDataSourceOstan.ConnectionString = DataAccess.Global.GetConnectionString();
            DrpCityHoghooghi.DataBind();
            DrpCityHaghighi.DataBind();
            DropDownCity.DataSource = DataAccess.TblCity.FetchAll(SubSonic.OrderBy.Asc("Name"));
            DropDownCity.DataTextField = "Name";
            DropDownCity.DataValueField = "ID";
            DropDownCity.DataBind();
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        try
        {
            BindingGrid();
        }
        catch
        { }
    }

    private void BindingGrid()
    {
        string orderExpression = DataAccess.TblUserDetail.Columns.Name;
        SubSonic.Query tempQuery = new Query(DataAccess.TblUserDetail.Schema);
        tempQuery.OrderBy = SubSonic.OrderBy.Asc("Name");
        if (ViewState["SortExpression"] != null && !ViewState["SortExpression"].ToString().Trim().Equals(String.Empty))
        {
            switch (ViewState["SortExpression"].ToString())
            {
                case "Name":
                    orderExpression = DataAccess.TblUserDetail.Columns.Name;
                    break;
                case "Family":
                    orderExpression = DataAccess.TblUserDetail.Columns.Family;
                    break;
                case "UserName":
                    orderExpression = DataAccess.TblUserDetail.Columns.UserName;
                    break;
            }

            if (ViewState["SortDirection"] != "Ascending")
                tempQuery.OrderBy = SubSonic.OrderBy.Desc(orderExpression);
            else
                tempQuery.OrderBy = SubSonic.OrderBy.Asc(orderExpression);
        }
        if (RadioButtonListRole.SelectedItem.Value == "Other")
        {
            SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
            q.QueryType = SubSonic.QueryType.Select;
            q.OrderBy = tempQuery.OrderBy;
            List<string> roles = new List<string>() {"Employer","FourthFactor","BonyadShahrestan","EditUser","ViewUser"};
            q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%")
                .AND(DataAccess.TblUserDetail.Columns.Role, Comparison.In, roles);
            DataSet dt = q.ExecuteDataSet();
            if (dt.Tables[0].Rows.Count > 0)
            {
                GridViewUsers.DataSource = dt;
                GridViewUsers.DataBind();
            }
            else
            {
                Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
            }
        }
        if (RadioButtonListRole.SelectedItem.Value == "Advisor")
        {
            if (RadioButtonListAdvisors.SelectedItem.Value == "hoghooghi")
            {
                SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                q.QueryType = SubSonic.QueryType.Select;
                q.OrderBy = tempQuery.OrderBy;
                q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhoghooghi");
                DataSet dt = q.ExecuteDataSet();
                if (dt.Tables[0].Rows.Count > 0)
                {
                    GridViewUsers.DataSource = dt;
                    GridViewUsers.DataBind();
                }
                else
                {
                    Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                }
            }
            if (RadioButtonListAdvisors.SelectedItem.Value == "haghighi")
            {
                SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                q.QueryType = SubSonic.QueryType.Select;
                q.OrderBy = tempQuery.OrderBy;
                q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhaghighi");
                DataSet dt = q.ExecuteDataSet();
                if (dt.Tables[0].Rows.Count > 0)
                {
                    GridViewUsers.DataSource = dt;
                    GridViewUsers.DataBind();
                }
                else
                {
                    Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                }
            }
        }
        if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
        {
            if (RadioButtonListSurveyor.SelectedItem.Value == "hoghooghi")
            {
                SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                q.QueryType = SubSonic.QueryType.Select;
                q.OrderBy = tempQuery.OrderBy;
                q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Surveyorhoghooghi");
                DataSet dt = q.ExecuteDataSet();
                if (dt.Tables[0].Rows.Count > 0)
                {
                    GridViewUsers.DataSource = dt;
                    GridViewUsers.DataBind();
                }
                else
                {
                    Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                }
            }
            if (RadioButtonListSurveyor.SelectedItem.Value == "haghighi")
            {
                SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
                q.QueryType = SubSonic.QueryType.Select;
                q.OrderBy = tempQuery.OrderBy;
                q.AddWhere(DataAccess.TblUserDetail.Columns.Family, SubSonic.Comparison.Like, "%" + TxtFamily.Text + "%");
                q.AddWhere(DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Surveyorhaghighi");
                DataSet dt = q.ExecuteDataSet();
                if (dt.Tables[0].Rows.Count > 0)
                {
                    GridViewUsers.DataSource = dt;
                    GridViewUsers.DataBind();
                }
                else
                {
                    Lblstatus.Text = " <script>alert('.فرد مورد نظر یافت نشد ')</script>";
                }
            }
        }
    }

    protected void GridViewUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName != "Sort")
        {
            try
            {
                int id = Convert.ToInt32(e.CommandArgument);
                switch (e.CommandName)
                {
                    case "Select":
                        Session["Roole"] = "other";
                        Guid userid = new Guid(GridViewUsers.DataKeys[id].Value.ToString());
                        string NameFamily = GridViewUsers.Rows[id].Cells[0].Text + " " +
                                            GridViewUsers.Rows[id].Cells[1].Text;
                        Session["UserId"] = userid;
                        IDataReader dr =
                            DataAccess.TblUserDetail.FetchByParameter(DataAccess.TblUserDetail.Columns.UserId, userid);
                        dr.Read();
                        if (RadioButtonListRole.SelectedItem.Value != "")
                            if (RadioButtonListRole.SelectedItem.Value == "Advisor")
                            {
                                RequiredFieldValidator18.Enabled = false;
                                LabelCity.Visible = false;
                                DropDownCity.Visible = false;
                                if (RadioButtonListAdvisors.SelectedItem.Value != "")
                                {
                                    if (RadioButtonListAdvisors.SelectedItem.Value == "hoghooghi")
                                    {
                                        DrpGender.Visible = false;
                                        LblGender.Visible = false;
                                        MultiViewCommon.ActiveViewIndex = 0;
                                        MultiViewNameFamily.ActiveViewIndex = -1;
                                        MultiViewAdvisors.ActiveViewIndex = 0;
                                    }
                                    else if (RadioButtonListAdvisors.SelectedItem.Value == "haghighi")
                                    {
                                        DrpGender.Visible = true;
                                        LblGender.Visible = true;
                                        MultiViewCommon.ActiveViewIndex = 0;
                                        MultiViewNameFamily.ActiveViewIndex = 0;
                                        MultiViewAdvisors.ActiveViewIndex = 1;
                                    }
                                }
                            }
                            else if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
                            {
                                RequiredFieldValidator18.Enabled = false;
                                LabelCity.Visible = false;
                                DropDownCity.Visible = false;
                                if (RadioButtonListSurveyor.SelectedItem.Value != "")
                                {
                                    if (RadioButtonListSurveyor.SelectedItem.Value == "hoghooghi")
                                    {
                                        DrpGender.Visible = false;
                                        LblGender.Visible = false;
                                        MultiViewCommon.ActiveViewIndex = 0;
                                        MultiViewNameFamily.ActiveViewIndex = -1;
                                        MultiViewAdvisors.ActiveViewIndex = 0;
                                    }
                                    else if (RadioButtonListSurveyor.SelectedItem.Value == "haghighi")
                                    {
                                        DrpGender.Visible = true;
                                        LblGender.Visible = true;
                                        MultiViewCommon.ActiveViewIndex = 0;
                                        MultiViewNameFamily.ActiveViewIndex = 0;
                                        MultiViewAdvisors.ActiveViewIndex = 1;
                                    }
                                }
                            }
                            else if (dr["Role"].ToString() == "BonyadShahrestan")
                            {
                                setdrops();
                                Session["Roole"] = "BonyadShahrestan";
                                RequiredFieldValidator18.Enabled = true;
                                LabelCity.Visible = true;
                                DropDownCity.Visible = true;
                            }
                            else
                            {
                                RequiredFieldValidator18.Enabled = false;
                                LabelCity.Visible = false;
                                DropDownCity.Visible = false;
                                setdrops();
                            }

                        DrpBranch.ClearSelection();
                        DrpBranchhaghighi.ClearSelection();
                        DrpCityHaghighi.ClearSelection();
                        DrpCityHoghooghi.ClearSelection();
                        DrpExtendedBranch.ClearSelection();
                        DrpGender.ClearSelection();
                        DrpGrade.ClearSelection();
                        DrpIsInEnginering.ClearSelection();
                        RegButton.Visible = true;
                        CancelButton.Visible = true;
                        ProfileCommon p = Profile.GetProfile(Membership.GetUser(Session["UserId"]).UserName);
                        MultiView1.ActiveViewIndex = -1;
                        TxtAddress.Text = p.Contacts.Address;
                        TxtTel.Text = p.Contacts.Phone1;
                        TxtName.Text = p.Common.FirstName;
                        TxtFamily0.Text = p.Common.LastName;
                        DrpGender.SelectedItem.Text = p.Common.Gender;
                        TxtEmail.Text = p.Contacts.Email;
                        TxtMobile.Text = p.Contacts.Mobile;
                        SqlDataSourceOstan.ConnectionString = DataAccess.Global.GetConnectionString();
                        DrpCityHoghooghi.DataBind();
                        DrpCityHaghighi.DataBind();
                        if (p.Common.City != "" && Session["Roole"].ToString() != "BonyadShahrestan")
                        {
                            for (int i = 0; i <= DrpCityHaghighi.Items.Count - 1; i++)
                            {
                                if (DrpCityHaghighi.Items[i].Text == p.Common.City.ToString().Trim())
                                    DrpCityHaghighi.Items[i].Selected = true;
                            }
                        }


                        if (p.HaghighiAdvisor.IsMemberofenginering != "")
                        {
                            for (int i = 0; i <= DrpIsInEnginering.Items.Count - 1; i++)
                            {
                                if (DrpIsInEnginering.Items[i].Text ==
                                    p.HaghighiAdvisor.IsMemberofenginering.ToString().Trim())
                                    DrpIsInEnginering.Items[i].Selected = true;
                            }
                        }
                        //DrpIsInEnginering.Items.FindByValue(p.HaghighiAdvisor.IsMemberofenginering.ToString().Trim()).Selected = true;

                        if (p.HaghighiAdvisor.GraduateGrade != "")
                            DrpGrade.Text = p.HaghighiAdvisor.GraduateGrade;
                        Business.Calender calender = new Business.Calender();
                        if (p.HaghighiAdvisor.GraduateDate != "")
                            TextBoxGraduate.Text =
                                calender.MiladiToPersian(Convert.ToDateTime(p.HaghighiAdvisor.GraduateDate).Date);
                        if (p.HaghighiAdvisor.Branch != "")
                            DrpBranchhaghighi.Items.FindByText(p.HaghighiAdvisor.Branch.ToString()).Selected = true;

                        if (dr["CityId"].ToString() != "")
                        {
                            DropDownCity.DataSource = DataAccess.TblCity.FetchAll(SubSonic.OrderBy.Asc("Name"));
                            DropDownCity.DataTextField = "Name";
                            DropDownCity.DataValueField = "ID";
                            DropDownCity.DataBind();

                            DataAccess.TblCity c = DataAccess.TblCity.FetchByID(dr["CityId"].ToString());
                            //DropDownCity.Items.FindByText(c.Name.Trim()).Selected = true;

                            for (int i = 0; i <= DropDownCity.Items.Count - 1; i++)
                            {
                                if (DropDownCity.Items[i].Text == c.Name.Trim())
                                    DropDownCity.Items[i].Selected = true;
                            }
                        }
                        dr.Close();
                        if (p.HoghooghiAdvisor.Branch != "")
                            for (int i = 0; i <= DrpBranch.Items.Count - 1; i++)
                            {
                                if (DrpBranch.Items[i].Text == p.HoghooghiAdvisor.Branch)
                                    DrpBranch.Items[i].Selected = true;
                            }
                        //DrpBranch.Items.FindByText(p.HoghooghiAdvisor.Branch.ToString()).Selected = true;


                        TxtCompanyName.Text = p.HoghooghiAdvisor.CompanyName;
                        TxtCompanyRegCode.Text = p.HoghooghiAdvisor.CompanyRegistrationCode;
                        TxtManager.Text = p.HoghooghiAdvisor.Manager;
                        if (p.HoghooghiAdvisor.ExtendedBranch != "")
                            for (int i = 0; i <= DrpExtendedBranch.Items.Count - 1; i++)
                            {
                                if (DrpExtendedBranch.Items[i].Text == p.HoghooghiAdvisor.ExtendedBranch)
                                    DrpExtendedBranch.Items[i].Selected = true;
                            }
                        //DrpExtendedBranch.Items.FindByText(p.HoghooghiAdvisor.ExtendedBranch.ToString()).Selected = true;


                        TxtCompanyWebSite.Text = p.Contacts.Site;
                        TxtworkYears.Text = p.Common.WorkYears;
                        if (p.HoghooghiAdvisor.ValidationDate != "")
                            TextBoxValidationDate.Text =
                                calender.MiladiToPersian(Convert.ToDateTime(p.HoghooghiAdvisor.ValidationDate).Date);

                        if (p.Common.City != "" && Session["Roole"].ToString() != "BonyadShahrestan")
                        {
                            for (int i = 0; i <= DrpCityHoghooghi.Items.Count - 1; i++)
                            {
                                if (DrpCityHoghooghi.Items[i].Text == p.Common.City.Trim())
                                    DrpCityHoghooghi.Items[i].Selected = true;
                            }
                        }
                        //

                        break;
                }
            }
            catch
            {
                Lblstatus.Text = " <script>alert('.  خطا در بارگذاری اطلاعات کاربر ')</script>";
            }
        }
    }
    public void setdrops()
    {
        LblGender.Visible = true;
        DrpGender.Visible = true;
        RadioButtonListAdvisors.Visible = false;
        RadioButtonListSurveyor.Visible = false;
        MultiViewCommon.ActiveViewIndex = 0;
        MultiViewNameFamily.ActiveViewIndex = 0;
        MultiViewAdvisors.ActiveViewIndex = -1;
    }
    protected void RadioButtonListAdvisors_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void PersianCalendar1_SelectionChanged(object sender, EventArgs e)
    {

        PopupControlExtender1.Commit(PersianCalendar1.SelectedDatePersian);

    }
    protected void PersianCalendar2_SelectionChanged(object sender, EventArgs e)
    {
        PopupControlExtender2.Commit(PersianCalendar2.SelectedDatePersian);

    }
    protected void RadioButtonListRole_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewUsers.DataBind();
        if (RadioButtonListRole.SelectedItem.Value == "Advisor")
        {
            RadioButtonListAdvisors.Visible = true;
            RadioButtonListSurveyor.Visible = false;
        }
        else if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
        {
            RadioButtonListAdvisors.Visible = false;
            RadioButtonListSurveyor.Visible = true;
        }
        else
        {

            RadioButtonListSurveyor.Visible = false;
            RadioButtonListAdvisors.Visible = false;
        }
    }
    protected void CancelButton_Click(object sender, EventArgs e)
    {
        Clear();

    }
    protected void RegButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (RadioButtonListRole.SelectedItem.Value == "Advisor")
                {
                    if (RadioButtonListAdvisors.SelectedItem.Value == "hoghooghi")
                    {
                        Userdetail(new Guid(Session["UserId"].ToString()), "Advisorhoghooghi");
                        ProfileSave(2);
                    }
                    else if (RadioButtonListAdvisors.SelectedItem.Value == "haghighi")
                    {
                        Userdetail(new Guid(Session["UserId"].ToString()), "AdvisorHaghighi");
                        ProfileSave(1);
                    }
                }

                else if (RadioButtonListRole.SelectedItem.Value == "Surveyor")
                {
                    if (RadioButtonListSurveyor.SelectedItem.Value == "hoghooghi")
                    {
                        Userdetail(new Guid(Session["UserId"].ToString()), "Surveyorhoghooghi");
                        ProfileSave(2);
                    }
                    else if (RadioButtonListSurveyor.SelectedItem.Value == "haghighi")
                    {
                        Userdetail(new Guid(Session["UserId"].ToString()), "SurveyorHaghighi");
                        ProfileSave(1);
                    }
                }
                else
                {
                    Userdetail(new Guid(Session["UserId"].ToString()), "Other");
                    ProfileSave(0);
                }
                Clear();
                RadioButtonListRole.ClearSelection();
                RegButton.Visible = false;
                CancelButton.Visible = false;
                Lblstatus.Text = " <script>alert('.  ثبت جزییات با موفقیت ثبت شد ')</script>";

            }
            catch
            {
                Lblstatus.Text = " <script>alert('.  ثبت جزییات با خطا مواجه شد ')</script>";

            }
        
    }
    public void Clear()
    {
        //for (int i = 0; i <= Form.Controls.Count; i++)
        //{
        //    Form.Controls[i].
        //}
        //foreach (Control container in Page.Controls) 
        //{ 

        //        foreach (Control ctrl in container.Controls) 
        //    {
        //            if (ctrl is TextBox) 
        //            {
        //                ((TextBox)ctrl).Text = ""; 
        //            } 
        //    }

        //}
        GridViewUsers.DataSource = "";
        GridViewUsers.DataBind();
        TxtAddress.Text = "";
        TxtCompanyName.Text = "";
        TxtCompanyRegCode.Text = "";
        TxtCompanyWebSite.Text = "";
        TxtEmail.Text = "";
        TxtFamily.Text = "";
        TxtFamily0.Text = "";
        TxtManager.Text = "";
        TxtMobile.Text = "";
        TxtName.Text = "";
        TxtTel.Text = "";
        TxtworkYears.Text = "";
        DrpBranch.ClearSelection();
        DrpBranchhaghighi.ClearSelection();
        DrpCityHaghighi.ClearSelection();
        DrpCityHoghooghi.ClearSelection();
        DrpExtendedBranch.ClearSelection();
        DrpGender.ClearSelection();
        DrpGrade.ClearSelection();
        DrpIsInEnginering.ClearSelection();
        MultiView1.ActiveViewIndex = 0;
        MultiViewAdvisors.ActiveViewIndex = -1;
        MultiViewCommon.ActiveViewIndex = -1;
        MultiViewNameFamily.ActiveViewIndex = -1;
        RadioButtonListSurveyor.Visible = false;
        RadioButtonListAdvisors.Visible = false;
        RegButton.Visible = false;
        CancelButton.Visible = false;
    }

    public void ProfileSave(int i)
    {
        ProfileCommon p = Profile.GetProfile(Membership.GetUser(Session["UserId"]).UserName);
        if (i == 0)
        {
            p.Contacts.Address = TxtAddress.Text;
            p.Contacts.Phone1 = TxtTel.Text;
            p.Common.FirstName = TxtName.Text;
            p.Common.LastName = TxtFamily0.Text;
            p.Common.Gender = DrpGender.SelectedItem.Text;
            p.Contacts.Email = TxtEmail.Text;
            p.Contacts.Mobile = TxtMobile.Text;
            if (Session["Roole"].ToString() == "BonyadShahrestan")
                p.Common.City = DropDownCity.SelectedItem.Value;
        }
        if (i == 1)
        {
            p.Contacts.Address = TxtAddress.Text;
            p.Contacts.Phone1 = TxtTel.Text;
            p.Common.FirstName = TxtName.Text;
            p.Common.LastName = TxtFamily0.Text;
            p.Common.Gender = DrpGender.SelectedItem.Text;
            p.Contacts.Email = TxtEmail.Text;
            p.Contacts.Mobile = TxtMobile.Text;
            p.Common.City = DrpCityHaghighi.SelectedItem.Text;
            p.HaghighiAdvisor.IsMemberofenginering = DrpIsInEnginering.SelectedItem.Text;
            p.HaghighiAdvisor.GraduateGrade = DrpGrade.SelectedItem.Text;
            string s = TextBoxGraduate.Text;
            string[] split = s.Split(new Char[] { '/' });
            Business.Calender calender = new Business.Calender();
            DateTime GraduateDate = calender.PersianToMiladi(split[0].ToString(), split[1].ToString(), split[2].ToString());
            p.HaghighiAdvisor.GraduateDate = GraduateDate.ToString();
            p.HaghighiAdvisor.Branch = DrpBranchhaghighi.SelectedItem.Text;
        }
        if (i == 2)
        {
            p.Contacts.Address = TxtAddress.Text;
            p.Contacts.Phone1 = TxtTel.Text;
            p.Common.Gender = DrpGender.SelectedItem.Text;
            p.Contacts.Email = TxtEmail.Text;
            p.Contacts.Mobile = TxtMobile.Text;
            p.HoghooghiAdvisor.Branch = DrpBranch.SelectedItem.Text;
            p.HoghooghiAdvisor.CompanyName = TxtCompanyName.Text;
            p.HoghooghiAdvisor.CompanyRegistrationCode = TxtCompanyRegCode.Text;
            p.HoghooghiAdvisor.Manager = TxtManager.Text;
            p.HoghooghiAdvisor.ExtendedBranch = DrpExtendedBranch.SelectedItem.Text;
            p.Contacts.Site = TxtCompanyWebSite.Text;
            p.Common.WorkYears = TxtworkYears.Text;
            string s = TextBoxValidationDate.Text;

            string[] split = s.Split(new Char[] { '/' });
            Business.Calender calender = new Business.Calender();
            DateTime ValidationDate = calender.PersianToMiladi(split[0].ToString(), split[1].ToString(), split[2].ToString());
            p.HoghooghiAdvisor.ValidationDate = ValidationDate.ToString();
            p.Common.City = DrpCityHoghooghi.SelectedItem.Text;
        }
        p.Save();

    }
    public void Userdetail(Guid userid, string role)
    {
        try
        {
            SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
            q.QueryType = SubSonic.QueryType.Update;
            q.AddUpdateSetting(DataAccess.TblUserDetail.Columns.Address, TxtAddress.Text);
            q.AddUpdateSetting(DataAccess.TblUserDetail.Columns.Mobile, TxtMobile.Text);
            q.AddUpdateSetting(DataAccess.TblUserDetail.Columns.Tel, TxtTel.Text);
            if (role == "Advisorhoghooghi")
            {
                q.AddUpdateSetting(DataAccess.TblUserDetail.Columns.Name, TxtCompanyName.Text);
                q.AddUpdateSetting(DataAccess.TblUserDetail.Columns.Family, TxtManager.Text);
            }
            else
            {
                if (Session["Roole"].ToString() == "BonyadShahrestan")
                    q.AddUpdateSetting(DataAccess.TblUserDetail.Columns.CityId, DropDownCity.SelectedItem.Value);
                q.AddUpdateSetting(DataAccess.TblUserDetail.Columns.Name, TxtName.Text);
                q.AddUpdateSetting(DataAccess.TblUserDetail.Columns.Family, TxtFamily0.Text);
            }
            q.AddWhere(DataAccess.TblUserDetail.Columns.UserId, userid);
            q.Execute();
        }
        catch
        {
            Lblstatus.Text = " <script>alert('.  ثبت جزییات با خطا مواجه شد ')</script>";
        }
    }

    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string GetDynamicContent(string contextKey)
    {
        return default(string);
    }
    protected void GridViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            IDataReader dr = DataAccess.TblUserDetail.FetchByParameter(DataAccess.TblUserDetail.Columns.UserName, e.Row.Cells[5].Text);
            if (dr.Read())
            {
                if (dr["Role"].ToString() == "Surveyorhaghighi")
                    e.Row.Cells[6].Text = "نقشه بردار حقیقی";
                if (dr["Role"].ToString() == "Surveyorhoghooghi")
                    e.Row.Cells[6].Text = "نقشه بردار حقوقی";
                if (dr["Role"].ToString() == "Advisorhaghighi")
                    e.Row.Cells[6].Text = "مشاور حقیقی";
                if (dr["Role"].ToString() == "Advisorhoghooghi")
                    e.Row.Cells[6].Text = "مشاور حقوقی";
                if (dr["Role"].ToString() == "Employer")
                    e.Row.Cells[6].Text = "کارفرما";
                if (dr["Role"].ToString() == "FourthFactor")
                    e.Row.Cells[6].Text = "عامل چهار";
                if (dr["Role"].ToString() == "BonyadShahrestan")
                    e.Row.Cells[6].Text = "بنیاد شهرستان";
                if (dr["Role"].ToString() == "EditUser")
                    e.Row.Cells[6].Text = "کاربر عادی ویرایش و ثبت";
                if (dr["Role"].ToString() == "ViewUser")
                    e.Row.Cells[6].Text = "کاربر عادی فقط مشاهده";
                dr.Close();
            }
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void GridViewUsers_OnSort(object sender, GridViewSortEventArgs e)
    {
        // get values from viewstate
        String sortExpression = ViewState["SortExpression"] as string;
        String sortDirection = ViewState["SortDirection"] as string;

        // on first time header clicked ( including different header), sort ASCENDING
        if (e.SortExpression != sortExpression)
        {
            sortExpression = e.SortExpression;
            sortDirection = "Ascending";
        }
        // on second time header clicked, toggle sort
        else
        {
            if (sortDirection == "Ascending")
            {
                sortExpression = e.SortExpression;
                sortDirection = "Descending";
            }
            // Descending
            else
            {
                sortExpression = e.SortExpression;
                sortDirection = "Ascending";
            }
        }

        // save state for next time

        ViewState["SortExpression"] = sortExpression;
        ViewState["SortDirection"] = sortDirection;
        BindingGrid();
    }

}
